package com.droid.lib.controls.demo.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.widget.RippleView;
import com.droid.lib.controls.widget.spinner.MaterialSpinner;
import com.github.johnpersano.supertoasts.library.Style;
import com.github.johnpersano.supertoasts.library.SuperActivityToast;
import com.github.johnpersano.supertoasts.library.SuperToast;

import static android.graphics.Typeface.BOLD;
import static android.graphics.Typeface.BOLD_ITALIC;
import static android.graphics.Typeface.ITALIC;

/**
 * 超级toast
 * Created by Administrator on 2016/9/29.
 */
public class ToastFragment extends Fragment {
    private Context mContext;
    private MaterialSpinner msTypefaceStyle, msAnimations, msFrame, msGravity, msDuration, msTextSize, msIconResource;
    private MaterialSpinner msActivityStyleType, msActivityTypefaceStyle, msActivityAnimations, msActivityFrame, msActivityGravity, msActivityDuration, msActivityButtonTextSize, msActivityTextSize, msActivityIconResource;
    private RippleView rv_show_toast, rv_show_activity_toast;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        View view = inflater.inflate(R.layout.fragment_toast, null);
        msTypefaceStyle = (MaterialSpinner) view.findViewById(R.id.ms_typeface_style);
        msAnimations = (MaterialSpinner) view.findViewById(R.id.ms_animations);
        msFrame = (MaterialSpinner) view.findViewById(R.id.ms_frame);
        msGravity = (MaterialSpinner) view.findViewById(R.id.ms_gravity);
        msDuration = (MaterialSpinner) view.findViewById(R.id.ms_duration);
        msTextSize = (MaterialSpinner) view.findViewById(R.id.ms_text_size);
        msIconResource = (MaterialSpinner) view.findViewById(R.id.ms_icon_resource);

        msTypefaceStyle.setItems(getResources().getStringArray(R.array.toast_typeface_style));
        msAnimations.setItems(getResources().getStringArray(R.array.toast_animations));
        msFrame.setItems(getResources().getStringArray(R.array.toast_frame));
        msGravity.setItems(getResources().getStringArray(R.array.toast_gravity));
        msDuration.setItems(getResources().getStringArray(R.array.toast_duration));
        msTextSize.setItems(getResources().getStringArray(R.array.toast_button_text_size));
        msIconResource.setItems(getResources().getStringArray(R.array.toast_iconresource));

        msActivityStyleType = (MaterialSpinner) view.findViewById(R.id.ms_activity_style_type);
        msActivityTypefaceStyle = (MaterialSpinner) view.findViewById(R.id.ms_activity_typeface_style);
        msActivityAnimations = (MaterialSpinner) view.findViewById(R.id.ms_activity_animations);
        msActivityFrame = (MaterialSpinner) view.findViewById(R.id.ms_activity_frame);
        msActivityGravity = (MaterialSpinner) view.findViewById(R.id.ms_activity_gravity);
        msActivityDuration = (MaterialSpinner) view.findViewById(R.id.ms_activity_duration);
        msActivityButtonTextSize = (MaterialSpinner) view.findViewById(R.id.ms_activity_button_text_size);
        msActivityTextSize = (MaterialSpinner) view.findViewById(R.id.ms_activity_text_size);
        msActivityIconResource = (MaterialSpinner) view.findViewById(R.id.ms_activity_icon_resource);

        msActivityStyleType.setItems(getResources().getStringArray(R.array.toast_style_type));
        msActivityTypefaceStyle.setItems(getResources().getStringArray(R.array.toast_typeface_style));
        msActivityAnimations.setItems(getResources().getStringArray(R.array.toast_animations));
        msActivityFrame.setItems(getResources().getStringArray(R.array.toast_frame));
        msActivityGravity.setItems(getResources().getStringArray(R.array.toast_gravity));
        msActivityDuration.setItems(getResources().getStringArray(R.array.toast_duration));
        msActivityButtonTextSize.setItems(getResources().getStringArray(R.array.toast_button_text_size));
        msActivityTextSize.setItems(getResources().getStringArray(R.array.toast_text_size));
        msActivityIconResource.setItems(getResources().getStringArray(R.array.toast_iconresource));

        rv_show_toast = (RippleView) view.findViewById(R.id.rv_show_toast);
        rv_show_activity_toast = (RippleView) view.findViewById(R.id.rv_show_activity_toast);
        rv_show_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SuperToast superToast = new SuperToast(getActivity());
                final int typefaceArbiter = msTypefaceStyle.getSelectedIndex() - 1;
                switch (typefaceArbiter) {
                    case 1:
                        superToast.setTypefaceStyle(BOLD);
                        break;
                    case 2:
                        superToast.setTypefaceStyle(BOLD_ITALIC);
                        break;
                    case 3:
                        superToast.setTypefaceStyle(ITALIC);
                        break;
                    default:
                        superToast.setTypefaceStyle(Typeface.NORMAL);
                }

                final int animationsArbiter = msAnimations.getSelectedIndex() - 1;
                switch (animationsArbiter) {
                    case 1:
                        superToast.setAnimations(Style.ANIMATIONS_FADE);
                        break;
                    case 2:
                        superToast.setAnimations(Style.ANIMATIONS_FLY);
                        break;
                    case 3:
                        superToast.setAnimations(Style.ANIMATIONS_POP);
                        break;
                    default:
                        superToast.setAnimations(Style.ANIMATIONS_SCALE);
                }

                final int frameArbiter = msFrame.getSelectedIndex() - 1;
                switch (frameArbiter) {
                    case 1:
                        superToast.setFrame(Style.FRAME_KITKAT);
                        break;
                    case 2:
                        superToast.setFrame(Style.FRAME_LOLLIPOP);
                        break;
                    default:
                        superToast.setFrame(Style.FRAME_STANDARD);
                        break;
                }


                final int gravityArbiter = msGravity.getSelectedIndex() - 1;
                switch (gravityArbiter) {
                    case 1:
                        superToast.setGravity(Gravity.TOP);
                        break;
                    case 2:
                        superToast.setGravity(Gravity.START);
                        break;
                    case 3:
                        superToast.setGravity(Gravity.END);
                        break;
                    default:
                        superToast.setGravity(Gravity.CENTER);
                        break;
                }

                final int durationArbiter = msDuration.getSelectedIndex() - 1;
                switch (durationArbiter) {
                    case 1:
                        superToast.setDuration(Style.DURATION_SHORT);
                        break;
                    case 2:
                        superToast.setDuration(Style.DURATION_LONG);
                        break;
                    default:
                        superToast.setDuration(Style.DURATION_MEDIUM);
                        break;
                }


                final int textSizeArbiter = msTextSize.getSelectedIndex() - 1;
                switch (textSizeArbiter) {
                    case 1:
                        superToast.setTextSize(Style.TEXTSIZE_SMALL);
                        break;
                    case 2:
                        superToast.setTextSize(Style.TEXTSIZE_LARGE);
                        break;
                    default:
                        superToast.setTextSize(Style.TEXTSIZE_MEDIUM);
                        break;
                }

                final int iconArbiter = msIconResource.getSelectedIndex() - 1;
                switch (iconArbiter) {
                    case 1:
                        superToast.setIconResource(Style.ICONPOSITION_BOTTOM, R.drawable.ic_github);
                        break;
                    case 2:
                        superToast.setIconResource(Style.ICONPOSITION_LEFT, R.drawable.ic_github);
                        break;
                    case 3:
                        superToast.setIconResource(Style.ICONPOSITION_RIGHT, R.drawable.ic_github);
                        break;
                    case 4:
                        superToast.setIconResource(Style.ICONPOSITION_TOP, R.drawable.ic_github);
                        break;
                    default:
                        break;
                }
                superToast.setText("Toast弹出提示信息");
                superToast.setColor(Color.argb(255, 255, 255, 255));
                superToast.setPriorityColor(Color.GREEN);
                superToast.setTextColor(Color.BLACK);
                superToast.show();
            }
        });
        rv_show_activity_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SuperActivityToast superActivityToast;
                final int typeArbiter = msActivityStyleType.getSelectedIndex() - 1;
                switch (typeArbiter) {
                    case 1:
                        superActivityToast = new SuperActivityToast(getActivity(), Style.TYPE_BUTTON);
                        break;
                    case 2:
                        superActivityToast = new SuperActivityToast(getActivity(), Style.TYPE_PROGRESS_BAR);
                        break;
                    case 3:
                        superActivityToast = new SuperActivityToast(getActivity(), Style.TYPE_PROGRESS_CIRCLE);
                        break;
                    default:
                        superActivityToast = new SuperActivityToast(getActivity());
                        break;
                }
                superActivityToast.setText("SuperActivityToast");
                superActivityToast.setTouchToDismiss(true);

                final int typefaceArbiter = msActivityTypefaceStyle.getSelectedIndex() - 1;
                switch (typefaceArbiter) {
                    case 1:
                        superActivityToast.setTypefaceStyle(BOLD);
                        break;
                    case 2:
                        superActivityToast.setTypefaceStyle(BOLD_ITALIC);
                        break;
                    case 3:
                        superActivityToast.setTypefaceStyle(ITALIC);
                        break;
                    default:
                        superActivityToast.setTypefaceStyle(Typeface.NORMAL);
                }

                final int animationsArbiter = msActivityAnimations.getSelectedIndex() - 1;
                switch (animationsArbiter) {
                    case 1:
                        superActivityToast.setAnimations(Style.ANIMATIONS_FADE);
                        break;
                    case 2:
                        superActivityToast.setAnimations(Style.ANIMATIONS_FLY);
                        break;
                    case 3:
                        superActivityToast.setAnimations(Style.ANIMATIONS_POP);
                        break;
                    default:
                        superActivityToast.setAnimations(Style.ANIMATIONS_SCALE);
                }


                final int frameArbiter = msActivityFrame.getSelectedIndex() - 1;
                switch (frameArbiter) {
                    case 1:
                        superActivityToast.setFrame(Style.FRAME_KITKAT);
                        break;
                    case 2:
                        superActivityToast.setFrame(Style.FRAME_LOLLIPOP);
                        break;
                    default:
                        superActivityToast.setFrame(Style.FRAME_STANDARD);
                        break;
                }


                final int gravityArbiter = msActivityGravity.getSelectedIndex() - 1;
                switch (gravityArbiter) {
                    case 1:
                        superActivityToast.setGravity(Gravity.TOP);
                        break;
                    case 2:
                        superActivityToast.setGravity(Gravity.START);
                        break;
                    case 3:
                        superActivityToast.setGravity(Gravity.END);
                        break;
                    default:
                        break;
                }

                final int durationArbiter = msActivityDuration.getSelectedIndex() - 1;
                switch (durationArbiter) {
                    case 1:
                        superActivityToast.setDuration(Style.DURATION_SHORT);
                        break;
                    case 2:
                        superActivityToast.setDuration(Style.DURATION_LONG);
                        break;
                    default:
                        superActivityToast.setDuration(Style.DURATION_MEDIUM);
                        break;
                }


                final int textSizeArbiter = msActivityTextSize.getSelectedIndex() - 1;
                switch (textSizeArbiter) {
                    case 1:
                        superActivityToast.setTextSize(Style.TEXTSIZE_SMALL);
                        break;
                    case 2:
                        superActivityToast.setTextSize(Style.TEXTSIZE_LARGE);
                        break;
                    default:
                        superActivityToast.setTextSize(Style.TEXTSIZE_MEDIUM);
                        break;
                }

                final int iconArbiter = msActivityIconResource.getSelectedIndex() - 1;
                switch (iconArbiter) {
                    case 1:
                        superActivityToast.setIconResource(Style.ICONPOSITION_BOTTOM, R.drawable.ic_github);
                        break;
                    case 2:
                        superActivityToast.setIconResource(Style.ICONPOSITION_LEFT, R.drawable.ic_github);
                        break;
                    case 3:
                        superActivityToast.setIconResource(Style.ICONPOSITION_RIGHT, R.drawable.ic_github);
                        break;
                    case 4:
                        superActivityToast.setIconResource(Style.ICONPOSITION_TOP, R.drawable.ic_github);
                        break;
                    default:
                        break;
                }

                if (superActivityToast.getType() == Style.TYPE_BUTTON) {
                    superActivityToast.setButtonText("UNDO");
                    superActivityToast.setButtonIconResource(R.drawable.ic_github);
                    superActivityToast.setOnButtonClickListener("good_tag_name", null, onButtonClickListener);

                    superActivityToast.setButtonDividerColor(Color.GRAY);

                    superActivityToast.setButtonTextColor(Color.BLACK);

                    final int buttonTextSizeArbiter = msActivityButtonTextSize.getSelectedIndex() - 1;
                    switch (buttonTextSizeArbiter) {
                        case 1:
                            superActivityToast.setButtonTextSize(Style.TEXTSIZE_SMALL);
                            break;
                        case 2:
                            superActivityToast.setButtonTextSize(Style.TEXTSIZE_LARGE);
                            break;
                        default:
                            superActivityToast.setButtonTextSize(Style.TEXTSIZE_MEDIUM);
                            break;
                    }
                } else if (superActivityToast.getType() == Style.TYPE_PROGRESS_BAR ||
                        superActivityToast.getType() == Style.TYPE_PROGRESS_CIRCLE) {
                    superActivityToast.setProgressIndeterminate(true);
                    superActivityToast.setProgressBarColor(Color.argb(255, 255, 255, 255));
                }
                superActivityToast.setPriorityColor(Color.GREEN);
                superActivityToast.setTextColor(Color.BLACK);
                superActivityToast.setColor(Color.argb(255, 255, 255, 255));
                superActivityToast.show();
            }
        });
        return view;
    }

    private final SuperActivityToast.OnButtonClickListener onButtonClickListener =
            new SuperActivityToast.OnButtonClickListener() {

                @Override
                public void onClick(View view, Parcelable token) {
                    SuperToast.create(view.getContext(), "OnClick!", Style.DURATION_SHORT)
                            .setPriorityLevel(Style.PRIORITY_HIGH).show();
                }
            };
}
